<template>
  <div id="cornerComment" v-if="dialogVisible">
    <div class="cornerComment-in" ref="dialogBox">
      <div class="cornerComment-in-title">
        <span>编辑家长信息</span>
        <span class="el-icon-circle-close close-Btn-line" @click="closePOpup"></span>
      </div>
      <div class="commentBody">
        <el-form :model="editForm" label-width="120px" :rules="editFormRules" ref="editForm">
          <el-form-item label="头像:" prop="pic">
            <imgUploadModle @getChildValue="getChildValue" imgNumber="1" page="teachingPic"
                            :picSrc="editForm.pic"></imgUploadModle>
          </el-form-item>
          <el-row>
            <el-col :span="10">
              <el-form-item label="家长姓名:" prop="name">
                <el-input v-model="editForm.name" auto-complete="off" placeholder="填写家长姓名"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="对应学生:" prop="student_id">
                {{ studentInfo ? studentInfo.name : '' }}
                <el-button @click="chooseStudent()" v-if="!parentsOption" size="mini">选择学生</el-button>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="10">
              <el-form-item label="关系:" prop="relation">
                <el-input v-model="editForm.relation" auto-complete="off" placeholder="填写孩子关系"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="电话:" prop="mobile_phone">
                <el-input v-model="editForm.mobile_phone" auto-complete="off" placeholder="填写电话"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="10">
              <el-form-item label="出生日期:" prop="birth">
                <el-input v-model="editForm.birth" auto-complete="off" placeholder="填写出生日期"></el-input>
              </el-form-item>
            </el-col>
<!--            <el-col :span="10">-->
<!--              <el-form-item label="身份证号:" prop="identity_card">-->
<!--                <el-input v-model="editForm.identity_card" auto-complete="off" placeholder="填写身份证号"></el-input>-->
<!--              </el-form-item>-->
<!--            </el-col>-->
          </el-row>
          <el-row>
            <el-col :span="10">
              <el-form-item label="居住地:" prop="adress">
                <el-input v-model="editForm.address" auto-complete="off" placeholder="填写居住地"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="工作单位及职务:" prop="word">
                <el-input v-model="editForm.work" auto-complete="off" placeholder="填写工作单位及职务"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div class="cornerComment-in-footer">
        <el-button @click="closePOpup" size="small">取消</el-button>
        <el-button @click="submitForm('editForm')" size="small" type="primary">确定并保存</el-button>
      </div>
    </div>
    <!--    全校单选学生 -->
    <singleStudentModle :visible.sync="studentVisible" @studentFn="studentFn"
                        :dynamicTagsArray="dynamicTagsArray"></singleStudentModle>
  </div>
</template>

<script>
import classInput from '@/components/template/classInput.vue'
import sectionInput from '@/components/template/sectionInput.vue'

export default {
  props: ['visible', 'parentsOption'], // commoent 学生点评 collection 学生采集
  components: {
    classInput,
    sectionInput
  },
  data() {
    return {
      dialogVisible: false,
      editForm: {
        pic: '',
        student_id: '', // 学号
        relation: '', // 关系
        name: '', // 姓名
        mobile_phone: '', // 电话
        work: '', // 工作职业
        birth: '', // 出生日期
        identity_card: '', //  	身份证
        address: '' //  	地址
      }, // 编辑界面数据
      usertype: '1',
      // 编辑界面数据
      editFormRules: {
        account: [
          {required: true, message: '请填写账户名', trigger: 'blur'}
        ],
        realname: [
          {required: true, message: '请填写姓名', trigger: 'blur'}
        ],
        role: [
          {required: true, message: '请选择身份', trigger: 'blur'}
        ]
      },
      role: [],
      rolePage: 1,
      classesvalue: '',
      studentVisible: false,
      dynamicTagsArray: [],
      studentInfo: ''
    }
  },
  watch: {
    visible() {
      this.dialogVisible = this.visible
      if (this.visible) {
        if (this.parentsOption) {
          this.InitFn()
        }
        this.roleFn()
      } else {
        for (let i in this.editForm) {
          this.editForm[i] = '';
        }
        this.studentInfo = ''
      }
    }
  },
  created() {
  },
  methods: {
    InitFn() {
      console.log('请求详情')
      this.$fetch(this.api.admin_parents_edit, {
        id: this.parentsOption.id
      }).then((response) => {
        if (response.code === 0) {
          this.editForm.pic = response.vo.pic
          this.editForm.student_id = response.vo.student.id
          this.editForm.relation = response.vo.relation
          this.editForm.name = response.vo.name
          this.editForm.mobile_phone = response.vo.mobile_phone?this.$verifyChekingMessage_notPass(response.vo.mobile_phone):''
          this.editForm.work = response.vo.work?this.$verifyChekingMessage_notPass(response.vo.work):''
          // this.editForm.identity_card = this.$verifyChekingMessage_notPass(response.vo.identity_card) // 身份证
          this.editForm.birth = response.vo.birth
          this.editForm.identity_card = response.vo.identity_card
          this.editForm.address = response.vo.address
          this.studentInfo = response.vo.student
        } else {
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    chooseStudent() {
      this.studentVisible = true
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (!this.studentInfo) {
            this.$message.error('请选择对应的学生')
          } else {
            this.submitFormAdd()
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    submitFormAdd() {
      let url = this.api.admin_parents_add
      if (this.parentsOption) {
        url = this.api.admin_parents_edit
        this.editForm.id = this.parentsOption.id
      }
      console.log('数据内容：', url, this.editForm)
      this.$post(url, this.editForm).then((response) => {
        if (response.code === 0) {
          this.$message.success('编辑成功')
          if (!this.parentsOption) {
            this.$emit('getevents')
            this.editForm.student_number = ''
            this.$refs['editForm'].resetFields()
          }
        } else {
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    getChildValue(option) {
      this.editForm.pic = option
    },
    // 关闭弹窗
    closePOpup() {
      this.$emit('update:visible', false)
    },
    // 收到学生信息
    studentFn(option) {
      console.log('收到数据内容：', option);
      this.dynamicTagsArray[0] = this.getJson(option);
      this.studentInfo = option;
      this.editForm.student_id = option.id;
      this.studentVisible = false;
    },
    getJson(item){
      return JSON.parse(JSON.stringify(item))
    },
    // 角色
    async roleFn() {
      let data = await this.$post(this.api.role.select, {});
      if (!data.code) {
        for (let i in data.list) {
          this.role.push(data.list[i])
        }
      }
    },
  },
  mounted() {
  }
}
</script>
<style scoped>
#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

.cornerComment-in {
  width: 60%;
  height: 60%;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
}

.commentBody {
  padding: 20px;
  flex: 1;
  overflow-y: auto;
}

.close-Btn-line {
  cursor: pointer;
}

</style>


